//
//  ZYLAuthorBaseViewController.m
//  Cartoon
//
//  Created by dllo on 15/7/14.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ZYLAuthorBaseViewController.h"
#import "ZYLAuthorTableView.h"
#import "Define.h"
#import "UIImageView+WebCache.h"
#import "ZYLAuthorTopicModel.h"
#import "ZYLAuthorModel.h"
@interface ZYLAuthorBaseViewController ()
@property (nonatomic, retain) ZYLAuthorTableView *authorTableView;
@property (nonatomic, retain) UIView *headerBackgroundView;
@property (nonatomic, retain) UIImageView *backgroundImageView;
@property (nonatomic, retain) UIView *myNavigationBackView;
@property (nonatomic, retain) ZYLAuthorModel *authorModel;
@property (nonatomic, retain) UILabel *authorIntro;
@end

@implementation ZYLAuthorBaseViewController

- (void)dealloc
{
    [_authorIntro release];
    [_authorId release];
    [_authorModel release];
    [_authorTableView release];
    [_headerBackgroundView release];
    [_backgroundImageView release];
    [_myNavigationBackView release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getAuthorModelWithAuthorID];
//    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = YES;
    
}


//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//   self.navigationController.navigationBar.hidden = YES;
//}
//- (void)viewWillDisappear:(BOOL)animated
//{
//    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.hidden = NO;
//}


#pragma mark - tabelheader
- (void)createAuthorTableView
{
    self.authorTableView = [[[ZYLAuthorTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain] autorelease];
    self.authorTableView.autorModel = self.authorModel;
    [self.view addSubview:self.authorTableView];
    self.headerBackgroundView = [[[UIView alloc] init] autorelease];
    self.headerBackgroundView.backgroundColor = [UIColor whiteColor];
    UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3.5)] autorelease];
    self.backgroundImageView.backgroundColor = [UIColor lightGrayColor];
    self.backgroundImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3.5)] autorelease];
    self.backgroundImageView.backgroundColor = [UIColor lightGrayColor];
    img.contentMode = UIViewContentModeScaleAspectFill;
    self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.backgroundImageView.clipsToBounds = YES;
    
    
    // 相同的图片 头像的上半部分背景图片
    self.backgroundImageView.image = [UIImage imageNamed:@""];
    self.backgroundImageView.image = [UIImage imageNamed:@""];
    
    
    [img addSubview:self.backgroundImageView];
    [self.headerBackgroundView addSubview:img];

    UIImageView *authorHeaderImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)] autorelease];
    authorHeaderImageView.layer.cornerRadius = 75;
    authorHeaderImageView.clipsToBounds = YES;
    authorHeaderImageView.center = CGPointMake(SCREEN_WIDTH / 4, self.backgroundImageView.frame.size.height / 5 * 4);
    [authorHeaderImageView sd_setImageWithURL:[NSURL URLWithString:self.authorModel.authoravatar_url]];
    [self.headerBackgroundView addSubview:authorHeaderImageView];
    
    
    
//    NSDictionary  *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:30], NSFontAttributeName, nil];
    
    
    /**********************************************/
#pragma mark 字符串计算方法
//    CGSize actualsize = [self.authorModel.authornickname boundingRectWithSize:CGSizeMake(300.f, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30]} context:nil].size;
//    NSLog(@"%f", actualsize.height);
    /**********************************************/
    UILabel *authorName = [[[UILabel alloc] initWithFrame:CGRectMake(authorHeaderImageView.frame.origin.x, authorHeaderImageView.frame.origin.y + authorHeaderImageView.frame.size.height + 20, [self.authorModel.authornickname boundingRectWithSize:CGSizeMake(MAXFLOAT, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30]} context:nil].size.width, 30)] autorelease];
    authorName.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:30];
    authorName.text = self.authorModel.authornickname;
    [self.headerBackgroundView addSubview:authorName];
    
    self.authorIntro = [[[UILabel alloc] initWithFrame:CGRectMake(authorHeaderImageView.frame.origin.x, authorName.frame.origin.y + authorName.frame.size.height + 10, SCREEN_WIDTH / 3 * 2, 20)] autorelease];
//    self.authorIntro.backgroundColor = [UIColor greenColor];
    self.authorIntro.font = [UIFont systemFontOfSize:15];
    self.authorIntro.text = self.authorModel.authorintro;
//    NSLog(@"%@", self.authorIntro.text);
    if (self.authorIntro.text == nil) {
        self.authorIntro.text = @"这个画家很懒,什么都没写";
    }
    [self.headerBackgroundView addSubview:self.authorIntro];
    
    self.headerBackgroundView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.authorIntro.frame.origin.y + self.authorIntro.frame.size.height + 20);
    self.authorTableView.tableHeaderView = self.headerBackgroundView;
    
    
    
    UIButton *introDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    introDetailButton.tag = 10000;
    introDetailButton.frame = CGRectMake(self.authorIntro.frame.origin.x + self.authorIntro.frame.size.width, self.authorIntro.frame.origin.y, 30, 30);
    introDetailButton.center = CGPointMake(introDetailButton.center.x, self.authorIntro.center.y);
    UIImageView *i = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iconfont-xiajiantou"]] autorelease];
    i.frame = CGRectMake(7.5, 5, 15, 20);

    [introDetailButton addSubview:i];
    [introDetailButton addTarget:self action:@selector(introDetailButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerBackgroundView addSubview:introDetailButton];
}

#pragma mark - 自定义navigationbar
- (void)createMyNavigationBar
{
    self.myNavigationBackView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)] autorelease];
    self.myNavigationBackView.backgroundColor = [UIColor orangeColor];
    self.myNavigationBackView.alpha = 0;
    [self.view addSubview:self.myNavigationBackView];
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setImage:[UIImage imageNamed:@"iconfont-fanhui-2"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(buttonBack) forControlEvents:UIControlEventTouchUpInside];
    back.frame = CGRectMake(8, 27, 30, 30);
    [self.view addSubview:back];
    CGSize titleSize = [self.authorModel.authornickname boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil].size;
    UILabel *naviTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleSize.width, 30)] autorelease];
    naviTitle.text = self.authorModel.authornickname;
    naviTitle.textAlignment = NSTextAlignmentCenter;
    naviTitle.font = [UIFont systemFontOfSize:17];
    naviTitle.center = CGPointMake(SCREEN_WIDTH / 2.0, back.center.y);
    [self.view addSubview:naviTitle];
    
    
    void(^block)(CGFloat) = ^(CGFloat f){
        self.myNavigationBackView.alpha = f / 100;
        if (f < 0) {
            self.backgroundImageView.frame = CGRectMake(0, f, SCREEN_WIDTH, SCREEN_HEIGHT / 3.5 - f);
        }
    };
    self.authorTableView.block = block;
}

#pragma mark - 详情箭头点击方法
- (void)introDetailButton:(UIButton *)button
{
    if (button.tag == 10000) {
        button.tag = 10001;
        self.authorIntro.numberOfLines = 0;
        self.authorIntro.frame = CGRectMake(self.authorIntro.frame.origin.x, self.authorIntro.frame.origin.y, self.authorIntro.frame.size.width, [self.authorModel.authornickname boundingRectWithSize:CGSizeMake(self.authorIntro.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height);
        self.headerBackgroundView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.authorIntro.frame.origin.y + self.authorIntro.frame.size.height + 20);
        self.authorTableView.tableHeaderView = self.headerBackgroundView;
        UIImageView *i = button.subviews.firstObject;
        i.image = [UIImage imageNamed:@"iconfont-shangjiantou"];
    } else if (button.tag == 10001) {
        button.tag = 10000;
        self.authorIntro.numberOfLines = 1;
        self.authorIntro.frame = CGRectMake(self.authorIntro.frame.origin.x, self.authorIntro.frame.origin.y, self.authorIntro.frame.size.width, 20);
        self.headerBackgroundView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.authorIntro.frame.origin.y + self.authorIntro.frame.size.height + 20);
        self.authorTableView.tableHeaderView = self.headerBackgroundView;
        UIImageView *i = button.subviews.firstObject;
        i.image = [UIImage imageNamed:@"iconfont-xiajiantou"];
    }
}

#pragma mark - 返回按钮方法
- (void)buttonBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 根据id获取作者信息model
- (void)getAuthorModelWithAuthorID
{
    [ZYLAuthorModel authorWithURLAndAuthorId:self.authorId block:^(ZYLAuthorModel *model) {
        self.authorModel = model;
        [self createAuthorTableView];
        [self createMyNavigationBar];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
